package com.example.moblie2021606143cwj0525;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox checkBoxmainPSW;
    private CheckBox checkBoxmainlogin;
    private Button btnmainlogin;
    private Button btnMainRegedit;
    private String usl = "http://121.9.253.237:8888/traffic/api/v2/user_login";
    OkHttpClient okHttpClient = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnmainlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=edtTxtMainUserName.getText().toString().trim();
                String password=edtTxtMainPassword.getText().toString().trim();
                 new Thread(new Runnable() {
                     @Override
                     public void run() {
                         JsonObject jsonObject=new JsonObject();
                         jsonObject.addProperty("UserName",password);
                         jsonObject.addProperty("UserPwd",password);
                         String json= jsonObject.toString();

                         MediaType mediaType= MediaType.Companion.parse("application/json;charset=utf-8");
                         RequestBody requestBody=RequestBody.create(json,mediaType);

                         Request request =new Request.Builder()
                                 .url(usl)
                                 .post(requestBody)
                                 .build();
                         Call call =okHttpClient.newCall(request);
                         call.enqueue(new Callback() {
                             @Override
                             public void onFailure(@NotNull Call call, @NotNull IOException e) {


                             }

                             @Override
                             public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                 String result =response.body().string();
                                 Gson gson=new Gson();
                                 LoginBean loginBean = gson.fromJson(result, LoginBean.class);
                                 runOnUiThread(new Runnable() {
                                     @Override
                                     public void run() {
                                         if(loginBean.getRESULT().equals("S")){
                                             new AlertDialog.Builder(MainActivity.this)
                                                     .setMessage("登陆成功")
                                                     .create()
                                                     .show();
                                         }else if(loginBean.getRESULT().equals("F")){
                                             new AlertDialog.Builder(MainActivity.this)
                                                     .setMessage("登陆失败")
                                                     .create()
                                                     .show();
                                         }
                                     }
                                 });
                                 Log.i("TAG", "onResponse: "+loginBean.getRESULT());
                             }
                         });

                     }
                 }).start();

            }
        });
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        checkBoxmainPSW = (CheckBox) findViewById(R.id.checkBoxmainPSW);
        checkBoxmainlogin = (CheckBox) findViewById(R.id.checkBoxmainlogin);
        btnmainlogin = (Button) findViewById(R.id.btnmainlogin);
        btnMainRegedit = (Button) findViewById(R.id.btnMainRegedit);
    }
}